import javax.swing.*;
import java.awt.*;

class BudgetManagement extends JFrame {
    private JTextField dailyBudgetField, monthlyBudgetField, yearlyBudgetField;
    private JButton saveButton, backButton;
    private FamilyFinanceManager mainFrame;

    public BudgetManagement(FamilyFinanceManager mainFrame) {
        this.mainFrame = mainFrame;
        setTitle("预算设定");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 200);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 2));

        panel.add(new JLabel("日预算:"));
        dailyBudgetField = new JTextField();
        panel.add(dailyBudgetField);

        panel.add(new JLabel("月预算:"));
        monthlyBudgetField = new JTextField();
        panel.add(monthlyBudgetField);

        panel.add(new JLabel("年预算:"));
        yearlyBudgetField = new JTextField();
        panel.add(yearlyBudgetField);

        saveButton = new JButton("保存");
        backButton = new JButton("返回");

        panel.add(saveButton);
        panel.add(backButton);

        add(panel, BorderLayout.CENTER);

        addListeners();
    }

    private void addListeners() {
        saveButton.addActionListener(e -> {
            String daily = dailyBudgetField.getText();
            String monthly = monthlyBudgetField.getText();
            String yearly = yearlyBudgetField.getText();
            if (!daily.isEmpty() && !monthly.isEmpty() && !yearly.isEmpty()) {
                DataManager.addBudget(daily, monthly, yearly);
                JOptionPane.showMessageDialog(this, "预算数据已保存");
                mainFrame.setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "请填写所有字段", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });

        backButton.addActionListener(e -> {
            mainFrame.setVisible(true);
            dispose();
        });
    }
}